package com.cy.store.controller;

import com.cy.store.entity.Order;
import com.cy.store.service.IOrderService;
import com.cy.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 订单的控制层
 */
@RestController
@RequestMapping("/orders")
public class OrderController extends BaseController {

    @Autowired
    private IOrderService orderService;

    @RequestMapping("/create")
    public JsonResult<Order> create(Integer aid, Integer[] cids, HttpSession session) {
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        Order order = orderService.create(aid, uid, username, cids);
        return new JsonResult<>(OK, order);
    }

    @RequestMapping("/find_by_oid")
    public JsonResult<Order> findByOid(Integer oid, HttpSession session) {
        Integer uid = getUidFromSession(session);
        Order order = orderService.findByOid(oid, uid);
        return new JsonResult<>(OK, order);
    }

    @RequestMapping("/pay_by_oid")
    @Transactional
    public JsonResult<Void> payByOid(Integer oid, Integer[] cids, HttpSession session) {
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        orderService.updateByOid(oid, username, uid, cids);
        return new JsonResult<>(OK);
    }
}
